<?php

include 'HttpClient.class.php';

//@ MEMBER_CODE：商户代码，登录飞印后在“API集成”->“获取API集成信息”获取
//@ FEYIN_KEY：密钥，获取方法同上
//@ DEVICE_NO：打印机设备编码，通过打印机后面的激活按键获取，为16位数字，例如"4600365507768327";
define('MEMBER_CODE', 'e691255424e611e393ba00163e02163b');
define('FEYIN_KEY', 'f0719283');
define('DEVICE_NO', '4600005741283715');

define('FEYIN_HOST','my.feyin.net');
define('FEYIN_PORT', 80);

class Feyin {
	
	public static function sendFreeMessage($msgInfo) {
// 		$msgInfo['msgNo'] = time() + 1;
		$msgInfo['reqTime'] = number_format(1000 * time(), 0, '', '');
		$msgInfo['memberCode'] = MEMBER_CODE;
		
		$content = $msgInfo['memberCode'].$msgInfo['msgDetail'].$msgInfo['deviceNo'].
			$msgInfo['msgNo'].$msgInfo['reqTime'].FEYIN_KEY;
		$msgInfo['securityCode'] = md5($content);
		$msgInfo['mode'] = 2;
	
		return self::sendMessage($msgInfo);
	}
	
	public static function sendFormatedMessage($msgInfo) {
// 		$msgInfo['msgNo'] = time() + 1;
		$msgInfo['reqTime'] = number_format(1000 * time(), 0, '', '');
		$msgInfo['memberCode'] = MEMBER_CODE;
		$msgInfo['deviceNo'] = DEVICE_NO;
		
		$content = $msgInfo['memberCode'].$msgInfo['customerName'].$msgInfo['customerPhone'].
			$msgInfo['customerAddress'].$msgInfo['customerMemo'].$msgInfo['msgDetail'].
			$msgInfo['deviceNo'].$msgInfo['msgNo'].$msgInfo['reqTime'].FEYIN_KEY;
	
		$msgInfo['securityCode'] = md5($content);
		$msgInfo['mode'] = 1;
		
		return self::sendMessage($msgInfo);
	}
	
	
	private static function sendMessage($msgInfo) {
		$client = new HttpClient(FEYIN_HOST,FEYIN_PORT);
		if(!$client->post('/api/sendMsg',$msgInfo)){ //提交失败
			Log::write('feyin post failed');
			return 'failed';
		}
		else{
			if (strcmp($client->getContent(), 0) != 0) {
				Log::write('feyin error code:' .$client->getContent());
			}
			return $client->getContent();
		}
	}
}

?>
